package marke.barak.windows.composit;


import java.io.FileNotFoundException;
import java.io.IOException;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import marek.barak.classes.Action;
import marek.barak.classes.ActionHddDAO;
import marek.barak.exceptions.InvalidActionValueException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Button;

public class AddActions extends Composite {
	private ActionHddDAO actions;
	private Text text;
	private Table table;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public AddActions(Composite parent, int style) {
		super(parent, style);
		setLayout(new GridLayout(3, false));
		try{
			actions = new ActionHddDAO();
			}catch(Exception ex){
				MessageBox msg = new MessageBox(getShell(),SWT.None|SWT.ICON_ERROR);
				msg.setMessage(ex.getStackTrace().toString());
				msg.setText("Error");
				msg.open();
			}
		
		text = new Text(this, SWT.BORDER);
		GridData gd_text = new GridData(SWT.LEFT, 1, false, false, 1, 1);
		gd_text.widthHint = 105;
		text.setLayoutData(gd_text);
		final Combo combo = new Combo(this, SWT.NONE);
		for(Action a: actions.getActions()){
			combo.add(a.getName());
		}
		combo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				for(TableItem item : table.getItems()){
					item.dispose();
				}
				text.setText("");
				
				List<Action> a = actions.getActions();
				
				for(Map.Entry<Integer, Integer> entry :a.get(combo.getSelectionIndex()).getTime().entrySet()){
					TableItem item = new TableItem(table,SWT.NONE);
					item.setText(new String[]{entry.getKey().toString(),entry.getValue().toString()});
				}
				text.setText(a.get(combo.getSelectionIndex()).getName());
				
				
			
				
			}
		});
		new Label(this, SWT.NONE);
		
		ToolBar toolBar = new ToolBar(this, SWT.FLAT | SWT.RIGHT);
		
		ToolItem tltmAdd = new ToolItem(toolBar, SWT.NONE);
		tltmAdd.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			try {
				Map<Integer,Integer> items = new TreeMap<Integer,Integer>();
				for(TableItem i: table.getItems()){
					items.put(Integer.parseInt((i.getText(0))), Integer.parseInt(i.getText(1)));
				}
				
				
					actions.addAction( new Action(text.getText(), items));
					combo.add(text.getText());
					
					
				} catch (FileNotFoundException e1) {
					MessageBox msg = new MessageBox(getShell(),SWT.NONE);
					msg.setText("Error");
					msg.setMessage(e1.getMessage());
				} catch (IOException e2) {
					MessageBox msg = new MessageBox(getShell(),SWT.NONE);
					msg.setText("Error");
					msg.setMessage(e2.getMessage());
				} catch (InvalidActionValueException e3){
					MessageBox msg = new MessageBox(getShell(),SWT.NONE);
					msg.setText("Error");
					msg.setMessage(e3.getMessage());
					msg.open();
				}finally{
					for(TableItem item :table.getItems()){
						item.dispose();
					}
					text.setText("");
				}
				
			}
			
		});
		tltmAdd.setText("ADD");
		
		ToolItem tltmSave = new ToolItem(toolBar, SWT.NONE);
		tltmSave.setText("SAVE");
		tltmSave.addSelectionListener(new SelectionAdapter() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				Map<Integer,Integer> items = new TreeMap<Integer,Integer>();
				for(TableItem i:table.getItems()){
					items.put(Integer.parseInt(i.getText(0)),Integer.parseInt(i.getText(1)));
				}
				try{
				actions.setActionTime(combo.getSelectionIndex(),items);
				for(TableItem i : table.getItems()){
					i.dispose();
				}
				text.setText("");
				
				
				}catch(IndexOutOfBoundsException ex){
					MessageBox msg= new MessageBox(getShell(),SWT.NONE);
					msg.setMessage(ex.getMessage());
					msg.setText("Error");
				}catch(InvalidActionValueException ex1){
					MessageBox msg = new MessageBox(getShell(),SWT.NONE);
					msg.setMessage(ex1.getMessage());
					msg.setText("Error");
				}catch(IOException ex2){
					MessageBox msg = new MessageBox(getShell(),SWT.None);
					msg.setMessage(ex2.getMessage());
					msg.setText("Error");
				}
			}
			
			
		});
		
		ToolItem tltmDelete = new ToolItem(toolBar, SWT.NONE);
		tltmDelete.setText("Delete");
		tltmDelete.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e){
				try{
				actions.removeSelectedAction(actions.getAction(combo.getSelectionIndex()));
				combo.remove(text.getText());
				text.setText("");
				for(TableItem i:table.getItems())
					i.dispose();
				}catch(InvalidActionValueException e1){
					MessageBox msg = new MessageBox(getShell(),SWT.NONE);
					msg.setMessage(e1.getMessage());
					msg.setText("Error");
				}catch(IOException e2){
					MessageBox msg = new MessageBox(getShell(),SWT.NONE);
					msg.setMessage(e2.getMessage());
					msg.setText("Error");
					getShell().close();
				}
			}
			
		});
		new Label(this, SWT.NONE);
		new Label(this, SWT.NONE);
		new Label(this, SWT.NONE);
		
		final Spinner spinner = new Spinner(this, SWT.BORDER);
		spinner.setMaximum(1000);
		GridData gd_spinner = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_spinner.widthHint = 86;
		spinner.setLayoutData(gd_spinner);
		
		final Spinner spinner_1 = new Spinner(this, SWT.BORDER);
		spinner_1.setMaximum(1000);
		GridData gd_spinner_1 = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_spinner_1.widthHint = 75;
		spinner_1.setLayoutData(gd_spinner_1);
		
		table = new Table(this, SWT.BORDER | SWT.FULL_SELECTION);
		GridData gd_table = new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1);
		gd_table.widthHint = 186;
		table.setLayoutData(gd_table);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		TableColumn tblclmnAmounth = new TableColumn(table, SWT.NONE);
		tblclmnAmounth.setWidth(100);
		tblclmnAmounth.setText("Amounth");
		
		TableColumn tblclmnDuration = new TableColumn(table, SWT.NONE);
		tblclmnDuration.setWidth(100);
		tblclmnDuration.setText("Duration");
	
		
	    Button btnNewButton = new Button(this, SWT.NONE);
	    btnNewButton.addSelectionListener(new SelectionAdapter() {
	    	@Override
	    	public void widgetSelected(SelectionEvent e) {
	    		if(spinner.getSelection()!=0 && spinner_1.getSelection()!=0){
	    			TableItem item =new TableItem(table,SWT.NONE);
	    			item.setText(new String[]{Integer.toString(spinner.getSelection()),Integer.toString(spinner_1.getSelection())});
	    		}
	    	}
	    });
	    btnNewButton.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, false, false, 1, 1));
	    btnNewButton.setText("Add Interval");
	    
	    Button btnNewButton_1 = new Button(this, SWT.NONE);
	    btnNewButton_1.addSelectionListener(new SelectionAdapter() {
	    	@Override
	    	public void widgetSelected(SelectionEvent e) {
	    		table.remove(table.getSelectionIndices());
	    	}
	    });
	  
	    btnNewButton_1.setLayoutData(new GridData(SWT.CENTER, SWT.TOP, false, false, 1, 1));
	    btnNewButton_1.setText("Delete interval");
	    
		for(Action a: actions.getActions())
			a.getName();
		

	}
	


	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
}

